﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Benefit.Models.ServerData
{
    /// <summary>
    /// 持仓汇总
    /// </summary>
    public class T_PositionSummaryDelivery
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 账号
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 合约
        /// </summary>
        public string InstrumentId { get; set; }
        /// <summary>
        /// 经纪公司代码
        /// </summary>
        public string BrokerId { get; set; }
        /// <summary>
        /// 投资者代码
        /// </summary>
        public string InvestorId { get; set; }
        /// <summary>
        /// 品种
        /// </summary>
        public string ProductId { get; set; }
        /// <summary>
        /// 交割期
        /// </summary>
        public string DeliveryDate { get; set; }
        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 多仓均价
        /// </summary>
        public double LongAvgprice { get; set; }
        /// <summary>
        /// 多仓保证金
        /// </summary>
        public double LongMargin { get; set; }
        /// <summary>
        /// 多仓手续费
        /// </summary>
        public double LongCommission { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 空仓均价
        /// </summary>
        public double ShortAvgprice { get; set; }
        /// <summary>
        /// 空仓保证金
        /// </summary>
        public double ShortMargin { get; set; }
        /// <summary>
        /// 空仓手续费
        /// </summary>
        public double ShortCommission { get; set; }
        /// <summary>
        /// 昨结算
        /// </summary>
        public double YesterdaySettlement { get; set; }
        /// <summary>
        /// 今结算
        /// </summary>
        public double SettlementPrice { get; set; }
        /// <summary>
        /// 持仓盯市盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 投保
        /// </summary>
        public string HedgeFlag { get; set; }
        /// <summary>
        /// 日期时间
        /// </summary>
        public double Datetime { get; set; }
        /// <summary>
        /// 逐笔持仓盯市盈亏
        /// </summary>
        public double PositionProfit2 { get; set; }
        /// <summary>
        /// 结算编号
        /// </summary>
        public int SettleMentID { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public string TradingDay { get; set; }

        ///// <summary>
        ///// 查询结算后的持仓汇总情况
        ///// </summary>
        ///// <param name="accountid"></param>
        ///// <param name="pdate"></param>
        ///// <returns></returns>
        //private System.Collections.Generic.List<T_PositionSummaryDelivery> GetInfo(string pdate)
        //{
        //    List<T_PositionSummaryDelivery> tpsd = new List<T_PositionSummaryDelivery>();
        //    T_Settle_History tsh = new T_Settle_History();
        //    int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
        //    if (settlementid != -1)
        //    {
        //        tpsd = GetPositionSummaryHistory(settlementid, pdate);
        //    }
        //    return tpsd;
        //}

        ///// <summary>
        ///// 查询某天最大结算编号的持仓汇总
        ///// </summary>
        ///// <param name="settlementid">结算编号</param>
        ///// <param name="pdate">日期</param>
        ///// <returns></returns>
        //private System.Collections.Generic.List<T_PositionSummaryDelivery> GetPositionSummaryHistory(int settlementid, string pdate)
        //{
        //    System.Collections.Generic.List<T_PositionSummaryDelivery> tpsd = new System.Collections.Generic.List<T_PositionSummaryDelivery>();
        //    string sql = "select * from T_PositionSummaryDelivery where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
        //    OracleDataReader sdr = Common.ADO.Tools.OracleHelper.ExecuteReader(sql);
        //    while (sdr.Read())
        //    {
        //        T_PositionSummaryDelivery s = null;
        //        s = new T_PositionSummaryDelivery();
        //        s.AccountId = Convert.ToInt32(sdr["AccountId"]);
        //        s.BrokerId = Convert.ToString(sdr["BrokerId"]);
        //        s.Datetime = Convert.ToDouble(sdr["Datetime"]);
        //        s.DeliveryDate = Convert.ToString(sdr["DeliveryDate"]);
        //        s.HedgeFlag = Convert.ToString(sdr["HedgeFlag"]);
        //        s.Id = Convert.ToInt32(sdr["Id"]);
        //        s.InstrumentId = Convert.ToString(sdr["InstrumentId"]);
        //        s.InvestorId = Convert.ToString(sdr["InvestorId"]);
        //        s.LongAvgprice = Convert.ToDouble(sdr["LongAvgprice"]);
        //        s.LongCommission = Convert.ToDouble(sdr["LongCommission"]);
        //        s.LongMargin = Convert.ToDouble(sdr["LongMargin"]);
        //        s.LongPosition = Convert.ToInt32(sdr["LongPosition"]);
        //        s.Margin = Convert.ToDouble(sdr["Margin"]);
        //        s.PositionProfit = Convert.ToDouble(sdr["PositionProfit"]);
        //        s.ProductId = Convert.ToString(sdr["ProductID"]);
        //        s.SettlementPrice = Convert.ToDouble(sdr["SettlementPrice"]);
        //        s.ShortAvgprice = Convert.ToDouble(sdr["ShortAvgprice"]);
        //        s.ShortCommission = Convert.ToDouble(sdr["ShortCommission"]);
        //        s.ShortMargin = Convert.ToDouble(sdr["ShortMargin"]);
        //        s.ShortPosition = Convert.ToInt32(sdr["ShortPosition"]);
        //        s.YesterdaySettlement = Convert.ToDouble(sdr["YesterdaySettlement"]);
        //        s.PositionProfit2 = Convert.ToDouble(sdr["PositionProfit2"]);
        //        s.SettleMentID = Convert.ToInt32(sdr["SettleMentID"]);
        //        s.TradingDay = sdr["TradingDay"].ToString();
        //        tpsd.Add(s);
        //    }
        //    sdr.Close();
        //    return tpsd;
        //}

        ///// <summary>
        ///// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        ///// </summary>
        ///// <param name="pdate">日期</param>
        //public void CopyServerPositionSummaryDataIntoBenefit(string pdate, DB.DBManager db)
        //{
        //    Data.ServerData.T_PositionSummaryDelivery h = new Data.ServerData.T_PositionSummaryDelivery();
        //    List<Data.ServerData.T_PositionSummaryDelivery> tsm = h.GetInfo(pdate);
        //    foreach (Data.ServerData.T_PositionSummaryDelivery ha in tsm)
        //    {
        //        db.PostionSummaryDelivery.Add(ha);
        //    }
        //    db.SaveChanges();
        //}


        ///// <summary>
        ///// 删除某日的所有持仓汇总记录
        ///// </summary>
        ///// <param name="pdate"></param>
        //public void DeleteDayPositionSummaryFromBenefit(string pdate, DB.DBManager db)
        //{
        //    var historys = from t in db.PostionSummaryDelivery where t.TradingDay.Equals(pdate) select t;
        //    foreach (Data.ServerData.T_PositionSummaryDelivery ha in historys)
        //    {
        //        db.PostionSummaryDelivery.Remove(ha);
        //    }
        //    db.SaveChanges();
        //}
    }
}
